<?php
/** The public php function for MySQL Database Connection
 *  @author wuguowen.
 */

class DBCONNECTION {
    var $dbhost = "";
    var $dbname = "";
    var $dbuser = "";
    var $dbpass = "";

    function DBCONNECTION($host, $name, $user, $pass) {
        $this->dbhost = $host;
        $this->dbname = $name;
        $this->dbuser = $user;
        $this->dbpass = $pass;
    }

    function connect() {
        global $CFG_DBDEBUG;

        if (! $dbh = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass)) {
            $err = "<h2>Unable to connect this database.</h2>";
            $err.= "<b>MySQL Error</b>:".mysql_error();
            if ($CFG_DBDEBUG) {
                echo $err;
                fn_log($err);
                die();
            } else {
                echo "<h2>Database error.</h2>";
            }
        }
        //set charset as utf8
        mysql_query("SET character_set_connection='utf8', character_set_results='utf8', character_set_client=binary", $dbh);
    
        if (! mysql_select_db($this->dbname)) {
            $err = "<h2>Unable to select this database.</h2>";
            $err.= "<b>MySQL Error</b>:".mysql_error();
            if ($CFG_DBDEBUG) {
                echo $err;
                fn_log($err);
                die();
            } else {
                echo "<h2>Database error.</h2>";
            }
        }

        return $dbh;
    }
    
    function close() {
        mysql_close();
    }
    
    function query($sql) {
        global $CFG_DBDEBUG,$TABLE_PREFIX;

        $sql  = fn_getParam1($sql);
        $rset = mysql_query($sql);
        if (! $rset) {
            $err = "<h2>Can't execute query</h2>";
            $err.= "<b>MySQL Error</b>: ".mysql_error();
            $err.= '<br/>Error SQL:'.str_replace($TABLE_PREFIX, '[Table]', $sql);

            if ($CFG_DBDEBUG) {
                echo $err;
                fn_log($err);
                die();
            } else {
                echo "<h2>Database error.</h2>";
            }
        }

        return $rset;
    }

    function fetchArray($rset) {
        return mysql_fetch_array($rset);
    }
    
    //if $isByName=false, then fetch row by index
    function fetchRow($rset,$isByName=true) {
        $aCols = array();

        if ($row = mysql_fetch_array($rset)) {
            for ($i=0; $i< mysql_num_fields($rset); $i++) {
                if ($isByName)
                    $colName = mysql_field_name($rset,$i);
                else
                    $colName = $i;
                $colType = mysql_field_type($rset,$i);
                if ($colType=="string" || $colType=="blob")
                    $aCols[$colName] = fn_setParam($row[$i]);
                else
                    $aCols[$colName] = $row[$i];
            }
            return $aCols;
        } else
            return false;
    }

    function getFirstField($sql) {
        $first= "";
        $rset = $this->query($sql);
        if ($row = mysql_fetch_array($rset)) {
            $colType = mysql_field_type($rset,0);
            if ($colType=="VARCHAR" || $colType=="TEXT")
                $first = fn_setParam($row[0]);
            else
                $first = $row[0];
        }
        
        mysql_free_result($rset);
        return $first;
    }
    
    function affectedRows() {
        return mysql_affected_rows();
    }
    
    function insertID() {
        return mysql_insert_id();
    }

    function freeResult($rset) {
        mysql_free_result($rset);
    }

    ////////the following code is transaction control/////////
    function tconnect() {
        global $CFG_DBDEBUG;

        if (! $dbh = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass)) {
            $err = "<h2>Unable to connect this database.</h2>";
            $err.= "<b>MySQL Error</b>:".mysql_error();
            if ($CFG_DBDEBUG) {
                echo $err;
                die();
            } else {
                echo "<h2>Database error.</h2>";
            }
            fn_log($err);
        }
    
        if (! mysql_select_db($this->dbname)) {
            $err = "<h2>Unable to select this database.</h2>";
            $err.= "<b>MySQL Error</b>:".mysql_error();
            if ($CFG_DBDEBUG) {
                echo $err;
                fn_log($err);
                die();
            } else {
                echo "<h2>Database error.</h2>";
            }
        }

        return $dbh;
    }
    //begin a transaction
    function tbegin($conn) {
        mysql_query("SET AUTOCOMMIT=0",$conn); //设置为不自动提交，因为MYSQL默认立即执行
        mysql_query("BEGIN",$conn); //开始事务定义
    }
    //execute insert/update/delete sql
    function tquery($sql,$conn) {
        global $CFG_DBDEBUG,$TABLE_PREFIX;

        $sql  = fn_getParam1($sql);
        $rset = mysql_query($sql,$conn);
        if (! $rset) {
            mysql_query("ROLLBACK",$conn);

            $err = "<h2>Can't execute query</h2>";
            $err.= "<b>MySQL Error</b>: ".mysql_error();
            $err.= '<br/>Error SQL:'.str_replace($TABLE_PREFIX, '[Table]', $sql);

            if ($CFG_DBDEBUG) {
                echo $err;
                fn_log($err);
                die();
            } else {
                echo "<h2>Database error.</h2>";
            }
        }

        return $rset;
    }
    //commit the transaction
    function tcommit($conn) {
        mysql_query("COMMIT",$conn); //commit transaction
    }
    //rollback the transaction
    function trollback($conn) {
        mysql_query("ROLLBACK",$conn); //rollback transaction
    }
    //close database connection
    function tclose($conn) {
        mysql_close($conn);
    }

}
?>